package com.webchat.search.service.voctor;


import com.webchat.common.constants.MilvusConstants;
import com.webchat.common.enums.AccountRelationTypeEnum;
import com.webchat.common.enums.RoleCodeEnum;
import com.webchat.common.exception.BusinessException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class MilvusServiceFactory implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;

    private static Map<String, AbstractMilvusService> services = new HashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        this.registryMilvusSearchService();
    }

    /**
     * 注册向量检索服务
     *
     */
    private void registryMilvusSearchService() {

        services.put(MilvusConstants.SearchBiz.SEARCH_ARTICLE.getBiz(), applicationContext.getBean(ArticleMilvusService.class));
    }

    public static AbstractMilvusService getService(String biz) {
        AbstractMilvusService abstractMilvusService = services.get(biz);
        if (abstractMilvusService != null) {
            return abstractMilvusService;
        }
        throw new BusinessException("不支持的向量检索服务");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
